<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_processes_engine
    //
	class t_field_processes_engine	extends t_base_field
	{
        // property declaration
		// public
		// E
		public $engine_type = 0;
		// L
		const language_position_start_label = 1;
		const language_position_stop_label = 2;
		const language_position_col_description = 3;
		const language_position_col_date = 4;
		const language_position_col_duration = 5;
		const language_position_col_result = 6;		
		const language_position_last = 7;
		
		// P
        const proper_properties_position_per_engine_type = 1;
		const proper_properties_position_per_control_buttons_visible = 2;
		const proper_properties_position_per_autostart = 3;	
        const proper_properties_position_per_timer_name = 4;		
		const proper_properties_position_per_timer_interval = 5;
		const proper_properties_position_per_use_custom_language = 6;
        const proper_properties_position_per_customized_result_file = 7;		
		const proper_properties_position_per_properties_process = 8;
        const proper_properties_position_last = 9;        
        
		//  L
		const engine_type_from_link = 1; //link from proper parameter
		const engine_type_from_link_query = 2; // link from sql query
		const engine_type_from_action = 3; // links from action
		const engine_type_execute_js_script = 4; // execute a js script
		// property declaration
		// private	
		// A
		private $execution_action_name = '';
		private $js_to_execute='';

		// property declaration
		// protected	
		// C
		protected $content_tag = null;
		// P
		protected $properties = '';

		// S
		protected $started = false;
		protected $start_button_tag = null;
		protected $start_button_onclick_content = 0;
		protected $stop_button_tag = null;
		protected $stop_button_onclick_content = 0;
		
		// T
		protected $table_tag = null;
		protected $tbody_tag = null;
		protected $timer_name = '';
        // method declaration
    	// protected
		
		// C
		protected function complete_field_tags()
        {			
			// start_button tag
			$local_label = $this->lang[self::language_position_start_label];
			$this->start_button_tag->set_text_to_include($local_label);	
			// stop_button tag
			$local_label = $this->lang[self::language_position_stop_label];
			$this->stop_button_tag->set_text_to_include($local_label);	
			
			
			


		}	
        // G
		protected function get_action_name($a_prefix)
        {
            return $a_prefix . "_" . $this->name;
        } 
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			$local_return = '';
			
			switch ($a_event_type) 
            {
                case t_base_field::field_event_type_on_selected:
					//echo "this->main_tag->name: $this->main_tag->name<br>";
					//$this->label_tag->add_property("onclick",t_cuteengine::javascript_instance_name.".".$this->name.".".$this->get_event_name($a_event_type)."($a_argouments)");
					
				break;                
			}
			return $local_return;
		}
                
        protected function get_language_js_array()
        {
            return '["'.implode('","',$this->lang).'"]';
        }    
				
					
static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()     
		{
            return self::proper_properties_position_last;
        }
		// G
		
		
		// I
		protected function is_started()
        {
            return ($this->started);
        }
		//S
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str) 
		{
			global $g_ambient_namespace;
			global $g_strings_utility_namespace;
			global $g_cuteengine;
			global $g_url_parameters_namespace;
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            // LANGUAGES
			//echo "this->proper_properties_array[self::proper_properties_position_per_use_custom_language]: ".$this->proper_properties_array[self::proper_properties_position_per_use_custom_language]."<br>";
            if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_use_custom_language]) == 'TRUE')
                $g_ambient_namespace->load_custom_language($this,$this->name);   
			// PROPRIERTIES	
			$this->properties = $this->proper_properties_array[self::proper_properties_position_per_properties_process];
			
			//$this->properties = $g_strings_utility_namespace->check_json_string_parameters(
				//$this,$this->name(),t_query_parameter::separator_level_1, $this->proper_properties_array[self::proper_properties_position_per_properties_process],
				//$this->get_proper_properties_position_last(),'PROPER PROPERTIES',true);
			$this->parent_component->content_loader->initialize_parameter('undefined',
				'undefined','undefined','undefined',
				'a_custom_on__error','undefined',false);
			// TIMERS	
			$this->timer_name = trim($this->proper_properties_array[self::proper_properties_position_per_timer_name]);	
			if ($this->timer_name == '')
			{
				$this->timer_name = t_cuteengine::timer_name;
				$this->timer = $g_cuteengine->add_timer($this->proper_properties_array[self::proper_properties_position_per_autostart],
					$this->proper_properties_array[self::proper_properties_position_per_timer_interval],10);
			}
			else
			{
				$this->timer = new t_html_timer($this->timer_name,
					$this->proper_properties_array[self::proper_properties_position_per_autostart],
					$this->proper_properties_array[self::proper_properties_position_per_timer_interval],10);
				
			}
			//echo "this->timer->name: ".$this->timer->name."<br>";
			$this->timer->add_components_name($this->name);
			// PROCESS ACTION
			$this->engine_type = $this->proper_properties_array[self::proper_properties_position_per_engine_type];
            $local_property_array = json_decode(str_replace('\\', '\\\\',$this->properties));
			if (($this->engine_type == self::engine_type_from_link) || ($this->engine_type == self::engine_type_from_link_query))
			{
				$this->execution_action_name = $this->get_action_name('get_contents');
				
				//echo substr($this->properties, 1, -1);
				/*$g_strings_utility_namespace->check_json_string_parameters(
					$this,$this->name(),t_query_parameter::separator_level_1, $this->properties,
					$this->get_proper_properties_position_last(),'',true);*/
				//$local_fields_array = $local_property_array[t_base_action::common_properties_position_per_field_list];
				$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_file_get_contents","'.$this->execution_action_name.'",["'.$this->name.'"],'.
//					str_replace($local_search, $local_replace,$g_url_parameters_namespace->add_argument_value_to_str(0,$this->properties,5)).']]');
					$g_url_parameters_namespace->add_argument_value_to_str(0,substr($this->properties, 1, -1),5).']');
					
			}
			else if ($this->engine_type == self::engine_type_execute_js_script)
			{
				//$local_property_array = json_decode(str_replace('\\', '\\\\',$this->properties));
				$this->js_to_execute = $local_property_array[0];
			}
			else
			{
				
				//$local_property_array = json_decode(str_replace('\\', '\\\\',$this->properties));
				/*$g_strings_utility_namespace->check_json_string_parameters(
					$this,$this->name(),t_query_parameter::separator_level_1, $this->properties,
					$this->get_proper_properties_position_last(),'',true);*/
				$local_fields_array = $local_property_array[t_base_action::common_properties_position_per_field_list];
				foreach ($local_fields_array As $i => $local_field)
				{
					$local_fields_array[$i] = $this->name;
				}
				$local_property_array[t_base_action::common_properties_position_per_field_list] = $local_fields_array;
				
				$this->execution_action_name = $local_property_array[t_base_action::common_properties_position_per_action_name];
				//echo "this->execution_action_name: $this->execution_action_name - $this->properties<br>";
				//print_r($local_property_array);
				
				$this->parent_part->add_action_for_field_uses_as_included(str_replace('\/','/',json_encode($local_property_array)));
			}	

		}
		// A
		
		public function apply_result_script($a_result,$a_current_row_no = -1,$a_count=-1)
        {
			
			if (($this->proper_properties_array[self::proper_properties_position_per_customized_result_file]) == '')
			{
				if ($a_count == -1)
					$a_result->add_function_result(
							"","","","
							var local_field = window.cuteengine['".$this->name."']; 
							local_field.end_execution('OK');
							");       
				else
					$a_result->add_function_result(
							"","","","
							var local_field = window.cuteengine['".$this->name."']; 
							local_field.end_execution('OK - $a_current_row_no - $a_count');
							");       
			}
			else
			{
				echo CUTEENGINE_PATH_CUSTOM_FILES . $this->proper_properties_array[self::proper_properties_position_per_customized_result_file].".php";
				require(CUTEENGINE_PATH_CUSTOM_FILES . $this->proper_properties_array[self::proper_properties_position_per_customized_result_file].".php");
			}
		}
		public function apply_error_result_script($a_result,$a_current_row_no = -1,$a_count=-1)
        {
			if (($this->proper_properties_array[self::proper_properties_position_per_customized_result_file]) == '')
			{
				$a_result->error_msg  = 'ERRORE';
					/*$a_result->add_function_result(
							"","","","
							var local_field = window.cuteengine['".$this->name."']; 
							local_field.end_execution('ERROR');
							"); */ 
				if ($a_count == -1)
					$a_result->add_function_result(
							"","","","
							var local_field = window.cuteengine['".$this->name."']; 
							local_field.end_execution('ERROR');
							");       
				else
					$a_result->add_function_result(
							"","","","
							var local_field = window.cuteengine['".$this->name."']; 
							local_field.end_execution('ERROR - $a_current_row_no - $a_count');
							"); 					
			
   			}
			else
			{
				echo CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR . $this->proper_properties_array[self::proper_properties_position_per_customized_result_file].".php";
				require(CUTEENGINE_PATH_CUSTOM_FILES . $this->proper_properties_array[self::proper_properties_position_per_customized_result_file].".php");
			}

		}
		
		// C		
		public function create_tags_structure()
        {
            global $g_javascript_namespace;
            if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_autostart]) == 'TRUE')
				$local_autostart = 'true';
			else	
				$local_autostart = 'false';
			//echo __METHOD__."<BR>";
            parent::create_tags_structure(
				// ARGUMENTS DECLARATIONS
                "a_timer_name,a_autostart",
                // ARGUMENTS VALUES
                "'$this->timer_name',$local_autostart");
			// proprierties
			// C
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'current_index',"-1",false);	
			// M	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'max_row',"60",false);	
			// I
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'incremental_index',"-1",false);	
			// S
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'start_button_tag',"this.name + '_start_button'",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'start_time',"new Date ()",false);
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
					get_class($this),'state',"'stopped'",false);	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'stop_button_tag',"this.name + '_stop_button'",false);
			// T	
			$g_javascript_namespace->header_script_generator->add_property_to_a_class(
                get_class($this),'timer_name',"a_timer_name",false);	
					
				
			
			// tags
			// content_tag
			$local_tag = parent::add_tag(t_base_tag_type::btt_div,
                $this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_content");
			$this->content_tag = $local_tag;
			
			if (strtoupper($this->proper_properties_array[self::proper_properties_position_per_control_buttons_visible]) == 'TRUE')
			{
				// buttons_panel tag
				$local_tag = $this->content_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_div,
					$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_buttons_panel");
				// start_button tag
				$this->start_button_tag = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_button,
					$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_start_button");
				$this->start_button_onclick_content = $g_javascript_namespace->header_script_generator->add_content_to_a_method(get_class($this), t_header_script_generator::create_label,"
		if (this.html_tag(this.start_button_tag) != null) this.html_tag(this.start_button_tag).onclick = this.start_process.set_scope(this);");				
				
				// stop_button tag
				$this->stop_button_tag = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_button,
					$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_stop_button");    
				$this->stop_button_onclick_content = $g_javascript_namespace->header_script_generator->add_content_to_a_method(get_class($this), t_header_script_generator::create_label,"
		if (this.html_tag(this.stop_button_tag) != null) this.html_tag(this.stop_button_tag).onclick = this.stop_process.set_scope(this);
		this.update_button();");				
			}	
			// results_panel tag
            $local_tag = $this->content_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_div,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_results_panel");
			
			// results_table tag
            $this->table_tag = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_table,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_results_table");
			// thead tag
            $local_tag = $this->table_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_thead,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_thead");
			// tr_head tag
            $local_tag = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_tr,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_tr_head");
			// th_desc tag
            $local_th = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_th,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."th_desc");  
			$local_th->set_text_to_include($this->lang[self::language_position_col_description]);	
			// th_date tag
            $local_th = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_th,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."th_date");  
			$local_th->set_text_to_include($this->lang[self::language_position_col_date]);	
			// th_duration tag
            $local_th = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_th,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."th_duration");  
			$local_th->set_text_to_include($this->lang[self::language_position_col_duration]);	
			// th_result tag
            $local_th = $local_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_th,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."th_result");
			$local_th->set_text_to_include($this->lang[self::language_position_col_result]);		
			// tbody tag
            $this->tbody_tag = $this->table_tag->create_and_add_child_at_bottom(t_base_tag_type::btt_tbody,
				$this->common_properties_array[t_base_field::common_properties_position_per_field_name]."_tbody");
			//	
			$this->main_tag = $this->content_tag;  
			$this->default_start_tag = $this->start_button_tag;
            //
			

			// event
			
			//$this->get_field_event_script(t_base_field::field_event_type_on_select,"event,".self::);
            
           // $this->default_result_tag = $local_current_tag;          
            //echo "field name: $this->name - default_start_tag->name: ".$this->default_start_tag->name."<BR>";
            //echo "MAIN FIELD TAG TROVATO";

			//if (! $this->is_started())
			//{
				
				$this->complete_field_tags();
			//}
			//else
			//{
				
			//}
			$g_javascript_namespace->header_script_generator->add_content_to_a_method(
					get_class($this),t_header_script_generator::create_label,"
	if (a_autostart == true)
		this.start_process();
	");	

			
        }
		// C
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
			global $g_cuteengine;
			$local_action = $g_cuteengine->actions_list[md5($this->execution_action_name)];
			$local_action->force_executable = true;
			
			$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'add_row','a_index'," 
			this.incremental_index ++;	
            if ((a_index < this.max_row) && (this.incremental_index < this.max_row))
			{
				
				var row = this.html_tag(this.name+'_tbody').insertRow (-1);
				row.name = this.name+'_r_'+a_index;
				row.id = row.name;
				var cell = row.insertCell (-1);
				cell.name = this.name+'_r_'+a_index+'_c_0';
				cell.id = cell.name;
				cell = row.insertCell (-1);
				cell.name = this.name+'_r_'+a_index+'_c_1';
				cell.id = cell.name;
				cell = row.insertCell (-1);
				cell.name = this.name+'_r_'+a_index+'_c_2';
				cell.id = cell.name;
				cell = row.insertCell (-1);
				cell.name = this.name+'_r_'+a_index+'_c_3';
				cell.id = cell.name;
				
			}
			if (a_index < this.max_row)
			{
				this.current_index = a_index;
			}
			else
			{
				this.current_index = 0;
			}
		");
		
			// E
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'end_execution','a_msg',"
		if (this.state == 'executing')
		{
			var local_time = new Date ();
			var local_delta = local_time - this.start_time; 
			local_time = new Date(local_delta);
			this.set_row_value(this.current_index,-1,-1,local_time.getHours()+':'+local_time.getMinutes()+':'+local_time.getSeconds(),a_msg);
			window.cuteengine[this.timer_name].stop_executing();
			this.state = 'started';
		}	
		");		
			$local_script = $this->js_to_execute;
			if ($this->execution_action_name != '')
			{
				$local_action = $g_cuteengine->actions_list[md5($this->execution_action_name)];
				$local_action->force_executable = true;
				$local_script .= $local_action->get_content_loader_code('',true,true).";";
			}
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'execute_process','a_custom_on__error'," 
		this.update_button();
		if (this.state == 'started')
		{
			this.state = 'executing';
			this.add_row(this.current_index + 1);
			this.start_time = new Date ();
			this.set_row_value(this.current_index,this.incremental_index,this.start_time.toLocaleDateString() + ' ' + this.start_time.toLocaleTimeString(),'','');
			var local_parent = '';

			". $local_action->get_content_loader_code('',true,true).";
		}");
		// I
			$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'is_to_execute',''," 
			var local_field = this;
			var local_time = new Date ();
			var local_delta = local_time - local_field.start_time; 
			if ((this.state == 'executing') && (local_delta > 300000))
				this.end_execution('TIMEOUT');
			return (this.state == 'started');
		");
			// S
		$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'set_row_value','a_index,a_operation,a_date,a_duration,a_result'," 
            if (a_operation != -1)
				this.html_tag(this.name+'_r_'+a_index+'_c_0').innerHTML = a_operation;
			if (a_date != -1)
				this.html_tag(this.name+'_r_'+a_index+'_c_1').innerHTML = a_date;
			if (a_duration != -1)	
				this.html_tag(this.name+'_r_'+a_index+'_c_2').innerHTML = a_duration;
			if (a_result != -1)
				this.html_tag(this.name+'_r_'+a_index+'_c_3').innerHTML = a_result;
			
		");			
		// O 
		$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
			get_class($this),'on_error',''," 
				window.cuteengine.add_err_log(this.content_loader.exception_error,'Field \"'+ this.name +'\" gets error: ');
				var local_field = this;
				local_field.end_execution('EXCEPTION');
					
				");
			$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'start_process','',"
		this.state = 'started';
		this.update_button();
		//window.cuteengine[this.timer_name].start_timer();
		");
			$g_javascript_namespace->header_script_generator->move_method_content(get_class($this),
				t_header_script_generator::create_label,$local_content, 11);	
			
			$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'stop_process',''," 
		this.state = 'stopped';	
		this.update_button();
		");
			$g_javascript_namespace->header_script_generator->move_method_content(get_class($this),
				t_header_script_generator::create_label,$local_content, 12);
		// U
		$local_content = $g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(	
				get_class($this),'update_button',''," 
		set_tag_property(['disabled'],[this.state != 'stopped'],this.start_button_tag,true);
		set_tag_property(['disabled'],[this.state == 'stopped'],this.stop_button_tag,true);
		");
			$g_javascript_namespace->header_script_generator->move_method_content(get_class($this),
				t_header_script_generator::create_label,$local_content, 13);
	
		}
		// F
        public function finalize_creation_field()
        {
			
            global $g_cuteengine;
			parent::finalize_creation_field();
			if ($this->is_started())
			{
				//echo "finalize_creation_field<br>";
				
				/*$this->link_tag->add_property("href",$this->get_link_value());	
                $local_result = new t_content_loader_result;
				$local_action = $g_cuteengine->actions_list[md5($this->get_action_name('get_report'))];
				$local_action->do_action_from_ajax($local_result);
				if (! $local_result->succeeded)
				{
					throw new Exception('action name: ' . $this->get_action_name('get_report') . ": " . $local_result->error_msg);
				}*/
				
			}
			
		}	
        // G
        public function get_event_name($a_event_type)
        {

            return /*get_class($this)*/$this->name . "_event_".$a_event_type;
        }
       /* public function get_event_type($a_ction_name)
        {	
			if ($a_ction_name == $this->get_action_name('get_report_from_event'))
				return t_base_field::field_event_type_on_selected;
			else
				return t_base_field::field_event_type_on_insert;
			
		}*/
		public function get_language_position_last()
        {
            return self::language_position_last;
        }

		public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }
        // I
		public function initialize_field_event($a_event_type)
        {			
			
            
		}		

    }
?>    